package com.jiaoyou.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiaoyou.enums.SexEnum;
import com.jiaoyou.mapper.UserInfoMapper;
import com.jiaoyou.pojo.User;
import com.jiaoyou.pojo.UserInfo;
import com.jiaoyou.result.R;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Service
public class UserInfoService {
    private static final Logger LOGGER =
            LoggerFactory.getLogger(UserInfoService.class);
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserService userService;
    @Autowired
    private FaceEngineService faceEngineService;

    @Value("${jiaoyou.path}") // 读取配置文件中的路径
    private String basePath;


    /**
     * 完善个人信息
     *
     * @return
     */
    public Boolean saveUserInfo(Map<String, String> param, String token) {
        User user = this.userService.queryUserByToken(token);
        if (user == null) {
            return false;
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setId(user.getId());
        userInfo.setSex(StringUtils.equals(param.get("gender"), "man") ?
                SexEnum.MAN : SexEnum.WOMAN);
        userInfo.setNickName(param.get("nickname"));
        userInfo.setBirthday(param.get("birthday"));
        userInfo.setCity(param.get("city"));
        // 保存UserInfo数据到数据库
        this.userInfoMapper.insert(userInfo);
        return true;
    }

    public Boolean saveLogo(MultipartFile file, String token) {
        User user = this.userService.queryUserByToken(token);
        if (user == null) {
            return false;
        }
        try {
        //校验头像是否为人像
            boolean isPortrait =
                    this.faceEngineService.checkIsPortrait(file.getBytes());
            if (!isPortrait) {
                return false;
            }
        } catch (Exception e) {
            LOGGER.error("检测人像图片出错!", e);
            return false;
        }
        // 图片上传到阿里云OSS
        //原始文件名
        String originalFinename =file.getOriginalFilename(); //会有重名现象
        String suffix = originalFinename.substring(originalFinename.lastIndexOf("."));//截取文件名.文件后缀

        //为了防止文件名重复使用uuid重新生成文件名称
        String fileName = UUID.randomUUID().toString()+ suffix;//生成随机字符串拼接原始文件文件后缀作为新文件名

        //创建一个目录对象
        File dir =new File(basePath);
        //判断当前目录是否存在
        if (!dir.exists()){
            //目录不存在创建一个目录
            dir.mkdirs();
        }

        //将临时文件转存到指定位置
        try {
            file.transferTo(new File(basePath+fileName)); //配置的固定路径加上原始的路径
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }
}